package com.qfedu.controller;

import com.qfedu.entity.PageBean;
import com.qfedu.entity.Product;
import com.qfedu.service.ProductService;
import com.qfedu.service.impl.ProductServiceImpl;
import com.qfedu.utils.BaseServlet;
import com.qfedu.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

@WebServlet("/product.do")
public class ProductServlet extends BaseServlet {
    private ProductService productService = new ProductServiceImpl();
    public String getProductByType(HttpServletRequest request, HttpServletResponse response){
        String tidStr = request.getParameter("tid");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        int tid = 0;
        int currentPage = 1;
        int pageSize = 8;
        if (tidStr != null){
            tid = Integer.valueOf(tidStr);
        }
        if (currentPageStr != null) {
            currentPage = Integer.valueOf(currentPageStr);
        }
        if (pageSizeStr != null){
            pageSize = Integer.valueOf(pageSizeStr);
        }
        try {
            PageBean<Product> pageBean = productService.getPageBean(tid, currentPage, pageSize);
            request.setAttribute("pageBean",pageBean);
            return Constants.FORWARD + "/goodsList.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }

    public String detail(HttpServletRequest request, HttpServletResponse response){
        String pidStr = request.getParameter("pid");
        int pid = 1;
        if (pidStr != null){
            pid = Integer.valueOf(pidStr);
        }
        try {
            Product product = productService.getProductById(pid);
            request.setAttribute("product",product);
            return Constants.FORWARD + "/goodsDetail.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }
}
